package com.ruoyi.admin.controller;


import com.ruoyi.common.annotation.NoRepeatSubmit;
import com.ruoyi.common.entity.LoginBody;
import com.ruoyi.common.entity.LoginUser;

import com.ruoyi.common.entity.system.SysUser;

import com.ruoyi.common.entity.vo.LoginUserVo;
import com.ruoyi.common.util.Result;
import com.ruoyi.common.util.ServletUtils;
import com.ruoyi.framework.service.SysLoginService;
import com.ruoyi.framework.service.SysPermissionService;
import com.ruoyi.framework.service.TokenService;
import com.ruoyi.system.service.ISysMenuService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.security.acl.LastOwnerException;
import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 */
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    @GetMapping("/captcha.jpg")
    public void captcha(HttpServletResponse response,String uuid) throws IOException {
        //设置响应头 不缓存
        response.setHeader("Cache-Control","no-store,no-cache");
        //设置响应格式为jpg
        response.setContentType("image/jpeg");

        //获取图片验证码 前端传进来的uuid需要保存进验证码实体类中
        BufferedImage image= loginService.getCode(uuid);
        //制作完返回
        ServletOutputStream outputStream=response.getOutputStream();
        ImageIO.write(image,"jpg",outputStream);
        IOUtils.closeQuietly(outputStream);
    }
    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public Result<?> login(@RequestBody @Validated LoginBody loginBody) {
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        return Result.success(token);
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public Result<?> getInfo() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        LoginUserVo loginUserVo=new LoginUserVo();
        loginUserVo.setPerms(permissions);
        loginUserVo.setSysUser(user);
        return Result.success(loginUserVo);
    }

}
